/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.viators.model;

import com.viators.beans.*;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.log4j.Logger;

/**
 *
 * @author Home
 */
public class GestionVehiculo {

    private static final Logger logger = Logger.getLogger(GestionVehiculo.class);

    public boolean insertarVehiculo(Sisma_vehiculoActionForm vaf,String accion) {
        boolean flag = false;
        try {
            String resource = "com/viators/xml/Conexion.xml";
            Reader reader = Resources.getResourceAsReader(resource);
            SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
            Sisma_vehiculoBean vb = new Sisma_vehiculoBean();
            vb.setNvehi_codigo(vaf.getNvehi_codigo());
            vb.setCvehi_marca(vaf.getCvehi_marca().toUpperCase());
            vb.setCvehi_modelo(vaf.getCvehi_modelo().toUpperCase());
            vb.setCvehi_numerochasis(vaf.getCvehi_numerochasis().toUpperCase());
            vb.setCvehi_numeromotor(vaf.getCvehi_numeromotor().toUpperCase());
            vb.setCvehi_numeroplaca(vaf.getCvehi_numeroplaca().toUpperCase());
            vb.setNvehi_fabricacion(vaf.getNvehi_fabricacion());
            vb.setNtive_codigo(vaf.getNtive_codigo());
            vb.setNpais_codigo(vaf.getNpais_codigo());
            
            if(accion.equals("nuevo")){
                logger.debug("insertarVehiculo ");
                sqlMap.insert("insertarVehiculo", vb);
            }else{
                logger.debug("modificarVehiculo ");
                sqlMap.update("modificarVehiculo", vb);                
            }

            flag = true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
        }
        return flag;
    }

    public boolean insertarVehiculoLista(Sisde_vehiculolistapasajerosBean vaf,String accion) {
        boolean flag = false;
        try {
            String resource = "com/viators/xml/Conexion.xml";
            Reader reader = Resources.getResourceAsReader(resource);
            SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
            Sisde_vehiculolistapasajerosBean vb = new Sisde_vehiculolistapasajerosBean();
            vb.setNvehi_codigo(vaf.getNvehi_codigo());
            vb.setNlipa_codigo(vaf.getNlipa_codigo());
            if(accion.equals("nuevo")){
                sqlMap.insert("insertarVehiculoLista", vb);
            }
            flag = true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
        }
        return flag;
    }

    public Collection listarTipoVehiculo() {
        List<TipoVehiculoBean> tiposvehiculo = new ArrayList<TipoVehiculoBean>();
        try {
            String resource = "com/viators/xml/Conexion.xml";
            Reader reader = Resources.getResourceAsReader(resource);
            SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
            List<TipoVehiculoBean> lista = sqlMap.queryForList("getTipoVehiculo", null);
            for (int i = 0; i < lista.size(); i++) {
                TipoVehiculoBean tipovehiculo = new TipoVehiculoBean();
                TipoVehiculoBean tv = (TipoVehiculoBean) lista.get(i);
                tipovehiculo.setNtive_codigo(tv.getNtive_codigo());
                tipovehiculo.setCtive_descripcion(tv.getCtive_descripcion().toUpperCase());
                tiposvehiculo.add(tipovehiculo);
            }
        } catch (Exception ex) {
            System.out.println("--->" + ex.getMessage());
            ex.printStackTrace();
        } finally {
        }
        return tiposvehiculo;
    }

    public int obtenerCodigoVehiculo() {
        int codigo = 0;
        try {
            String resource = "com/viators/xml/Conexion.xml";
            Reader reader = Resources.getResourceAsReader(resource);
            SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
            Sisma_vehiculoBean vehiculo = (Sisma_vehiculoBean) sqlMap.queryForObject("getIdNewVehiculo", null);
            codigo = vehiculo.getNvehi_codigo();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
        }
        return codigo;
    }
}
